/*
Date: October 2025
Project: Income and Child Maltreatment: Evidence from a Discontinuity in Tax Benefits
Author: Katherine Rittenhouse
Purpose: This file builds the dataset used for analysis.
Files in: samplepluspredictions; historical-poverty-guidelines.xlsx; CPI.dta
Files out: analysis
*/

clear all
set more off

set maxvar 120000

use "samplepluspredictions.dta",clear

forval x = 0/8 {
replace numrefage`x' = 0 if numrefage`x'==.
replace numinvage`x' = 0 if numinvage`x'==.
replace numsuage`x' = 0 if numsuage`x' ==.
replace daysplage`x' = 0 if daysplage`x'==.
replace anyplage`x'=0 if anyplage`x'==.
foreach cat in "physical" "neglect" "emotional" "other" {
replace numal`cat'_`x' = 0 if numal`cat'_`x'==.
replace numsu`cat'_`x' = 0 if numsu`cat'_`x'==.
}
foreach rep in "other" "school" "nonmand" "legprof" "medprof" "cws" {
replace numrep`rep'age`x' = 0 if numrep`rep'age`x' == .
}
}

replace numreferrals = 0 if numreferrals == .
local var numrefage
gen numref0_2 = `var'0 + `var'1 +`var'2
gen numref3_5 = `var'3 + `var'4 + `var'5
gen numref6_8 = `var'6+`var'7+`var'8

gen anyref0_2 = (numref0_2>0)
gen anyref3_5 = (numref3_5>0)
gen anyref6_8 = (numref6_8>0)

local var numinvage
gen numinv0_2 = `var'0 + `var'1 +`var'2
gen numinv3_5 = `var'3 + `var'4 + `var'5
gen numinv6_8 = `var'6+`var'7+`var'8


gen anyinv0_2 = (numinv0_2>0)
gen anyinv3_5 = (numinv3_5>0)
gen anyinv6_8 = (numinv6_8>0)


local var daysplage
gen dayspl0_2 = `var'0 + `var'1 +`var'2
gen dayspl3_5 = `var'3 + `var'4 + `var'5
gen dayspl6_8 = `var'6+`var'7+`var'8

gen anypl0_2= dayspl0_2>0
gen anypl3_5= dayspl3_5>0
gen anypl6_8= dayspl6_8>0

local var numsuage
gen numsu0_2 = `var'0 + `var'1 +`var'2
gen numsu3_5 = `var'3 + `var'4 + `var'5
gen numsu6_8 = `var'6+`var'7+`var'8

gen anysu0_2 = (numsu0_2>0)
gen anysu3_5 = (numsu3_5>0)
gen anysu6_8 = (numsu6_8>0)

gen anysubstal0_2 = (numsu0_2>0 & numsu0_2!=.)

foreach cat in "physical" "neglect" "emotional" "other" {
local var numal`cat'_
gen numal`cat'0_2 = `var'0 + `var'1 +`var'2
gen numal`cat'3_5 = `var'3 + `var'4 + `var'5
gen numal`cat'6_8 = `var'6+`var'7+`var'8

local var numsu`cat'_
gen numsu`cat'0_2 = `var'0 + `var'1 +`var'2
gen numsu`cat'3_5 = `var'3 + `var'4 + `var'5
gen numsu`cat'6_8 = `var'6+`var'7+`var'8
}
foreach rep in "other" "nonmand" "legprof" "medprof" "school" "cws" {
local var numrep`rep'age
gen numrep`rep'0_2 = `var'0 + `var'1 +`var'2
gen numrep`rep'3_5 = `var'3 + `var'4 + `var'5
gen numrep`rep'6_8 = `var'6+`var'7+`var'8
}
foreach var in "anyref" "numref" "numalneglect" "numalphysical" "numsuneglect" "numsuphysical"  "numalemotional" "numalother" "numinv" "anyinv" "numrepother" "numrepnonmand" "numreplegprof" "numrepmedprof" "numrepschool" "numrepcws" "numsu" "anysu" "dayspl" "anypl" {
replace `var'0_2 = . if recentered_yr>2017
replace `var'3_5 = . if recentered_yr>2014
replace `var'6_8 = . if recentered_yr>2011
}

save "analysis.dta",replace

import excel using  "historical-poverty-guidelines.xlsx",firstrow clear
keep B D E 
drop if B=="" |B=="Year"
ren B recentered_yr 
ren E povline_3person
ren D povline_2person
destring recentered_yr povline*,force replace 

merge 1:m recentered_yr using "analysis.dta",gen(_povm)
drop if _povm!=3
drop _povm


merge m:1 recentered using "CPI.dta",gen(_cpim)
keep if _cpim==3
drop _cpim
gen CPI2017=361
gen realinc = aftertax*(CPI2017/CPI)
gen realval = value*(CPI2017/CPI)
gen lowinc = (realinc<40840 & realinc!=.)
replace lowinc = . if realinc==.

ren (aftertax value) (unadjinc unadjval)
ren (realinc realval) (aftertax_hat value_hat)

keep if recentered<=2017

save "analysis.dta",replace
